package com.example.system.manger;


import com.example.common.core.constants.CacheConstants;
import com.example.redis.service.RedisService;
import com.example.system.domain.exam.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExamCacheManger {
    @Autowired
    private RedisService redisService;

    //往redis里添加竞赛信息
    public void addCache(Exam exam) {
        redisService.leftPushForList(getExamListKey(), exam.getExamId());
        redisService.setCacheObject(getDetailKey(exam.getExamId()) ,exam );

    }

    //从redis里删除竞赛信息
    public void deleteCache(Long examId) {
        //删除竞赛列表里的examId
        redisService.removeForList(getExamListKey(), examId);
        //删除竞赛详情列表里的examId
        redisService.deleteObject(getDetailKey(examId));
        redisService.deleteObject(getExamQuestionListKey(examId));
    }

    //获取竞赛列表里的键
    private String getExamListKey() {
        return CacheConstants.EXAM_UNFINISHED_LIST;
    }

    //获取竞赛详情里的键
    public String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL+examId;
    }

    //生成题目竞赛列表的key
    private String getExamQuestionListKey(Long examId) {
        return CacheConstants.EXAM_QUESTION_LIST + examId;
    }
}
